package web.suzy.oj.manager.admin.tag;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import web.suzy.oj.common.exception.StatusFailException;
import web.suzy.oj.dao.problem.TagClassificationEntityService;
import web.suzy.oj.dao.problem.TagEntityService;
import web.suzy.oj.pojo.entity.problem.Tag;
import web.suzy.oj.pojo.entity.problem.TagClassification;
import web.suzy.oj.shiro.AccountProfile;
import web.suzy.oj.validator.CommonValidator;

import java.util.List;

/**
 * YangSuzy 软件工作室
 * 类名: AdminTagManager
 * 描述: ---- 待定 -----
 * 功能: ---- 待定 -----
 *
 * @author YangSuzy
 * Date: 2022/11/28 13:53
 */
@Component
@Slf4j(topic = "suzyoj")
public class AdminTagManager {
    @Autowired
    private TagEntityService tagEntityService;

    @Autowired
    private TagClassificationEntityService tagClassificationEntityService;

    @Autowired
    private CommonValidator commonValidator;

    /**
     * 方法名: addTag
     * 描述: 添加题目标签
     *
     * @param tag 标签
     * @return web.suzy.oj.pojo.entity.problem.Tag
     * @date 2022/11/28 13:54
     * @auther YangSuzy
     **/
    public Tag addTag(Tag tag) throws StatusFailException {
        commonValidator.validateContent(tag.getName(), "标签名", 255);
        //校验是否存在该标签
        QueryWrapper<Tag> tagQueryWrapper = new QueryWrapper<>();
        tagQueryWrapper.eq("name", tag.getName());
        Tag existTag = tagEntityService.getOne(tagQueryWrapper, false);
        if (existTag != null) {
            throw new StatusFailException("该标签名称已存在！请勿重复添加！");
        }

        boolean isOk = tagEntityService.save(tag);
        if (!isOk) {
            throw new StatusFailException("添加失败");
        }
        return tag;
    }

    /**
     * 方法名: updateTag
     * 描述: 更新题目标签
     *
     * @param tag 标签
     * @return void
     * @date 2022/11/28 14:38
     * @auther YangSuzy
     **/
    public void updateTag(Tag tag) throws StatusFailException {
        commonValidator.validateContent(tag.getName(), "标签名", 255);
        boolean isOk = tagEntityService.updateById(tag);
        if (!isOk) {
            throw new StatusFailException("更新失败");
        }
    }

    /**
     * 方法名: deleteTag
     * 描述: 删除题目标签
     *
     * @param tid 标签ID
     * @return void
     * @date 2022/11/28 14:42
     * @auther YangSuzy
     **/
    public void deleteTag(Long tid) throws StatusFailException {
        boolean isOk = tagEntityService.removeById(tid);
        if (!isOk) {
            throw new StatusFailException("删除失败");
        }
        AccountProfile userRolesVo = (AccountProfile) SecurityUtils.getSubject().getPrincipal();
        log.info("[{}],[{}],tid:[{}],operatorUid:[{}],operatorUsername:[{}]", "Admin_Tag", "Delete", tid, userRolesVo.getUid(), userRolesVo.getUsername());
    }

    /**
     * 方法名: getTagClassification
     * 描述: 获取标签分类
     *
     * @return java.util.List<web.suzy.oj.pojo.entity.problem.TagClassification>
     * @date 2022/11/29 12:17
     * @auther YangSuzy
     **/
    public List<TagClassification> getTagClassification() {
        QueryWrapper<TagClassification> tagClassificationQueryWrapper = new QueryWrapper<>();
        //根据排名降序排序
        tagClassificationQueryWrapper.orderByAsc("`rank`");
        return tagClassificationEntityService.list(tagClassificationQueryWrapper);
    }

    /**
     * 方法名: addTagClassification
     * 描述: 添加标签分类
     *
     * @param tagClassification 标签分类
     * @return web.suzy.oj.pojo.entity.problem.TagClassification
     * @date 2022/11/29 13:13
     * @auther YangSuzy
     **/
    public TagClassification addTagClassification(TagClassification tagClassification) throws StatusFailException {
        commonValidator.validateContent(tagClassification.getName(), "标签分类名", 255);
        QueryWrapper<TagClassification> tagClassificationQueryWrapper = new QueryWrapper<>();
        tagClassificationQueryWrapper.eq("name", tagClassification.getName());
        TagClassification existTagClassification = tagClassificationEntityService.getOne(tagClassificationQueryWrapper, false);

        if (existTagClassification != null) {
            throw new StatusFailException("该标签分类名称已存在！请勿重复！");
        }
        boolean isOk = tagClassificationEntityService.save(tagClassification);
        if (!isOk) {
            throw new StatusFailException("添加失败");
        }
        return tagClassification;
    }

    /**
     * 方法名: updateTagClassification
     * 描述: 更新标签分类
     *
     * @param tagClassification 标签分类
     * @return void
     * @date 2022/11/29 13:17
     * @auther YangSuzy
     **/
    public void updateTagClassification(TagClassification tagClassification) throws StatusFailException {
        commonValidator.validateContent(tagClassification.getName(), "标签分类名", 255);
        boolean isOk = tagClassificationEntityService.updateById(tagClassification);
        if (!isOk) {
            throw new StatusFailException("更新失败");
        }
    }

    /**
     * 方法名: deleteTagClassification
     * 描述: 删除标签分类
     *
     * @param tcid 标签分类ID
     * @return void
     * @date 2022/11/29 13:19
     * @auther YangSuzy
     **/
    public void deleteTagClassification(Long tcid) throws StatusFailException {
        boolean isOk = tagClassificationEntityService.removeById(tcid);
        if (!isOk) {
            throw new StatusFailException("删除失败");
        }
        AccountProfile userRolesVo = (AccountProfile) SecurityUtils.getSubject().getPrincipal();
        log.info("[{}],[{}],tcid:[{}],operatorUid:[{}],operatorUsername:[{}]", "Admin_Tag_Classification", "Delete", tcid, userRolesVo.getUid(), userRolesVo.getUsername());
    }
}
